package wowraidmanager.client.wishlist.user;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.client.service.wishlist.MemberService;
import wowraidmanager.client.service.wishlist.MemberServiceAsync;
import wowraidmanager.client.wishlist.utils.CheckError;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Createuser extends BaseComposite{

    private static CreateuserUiBinder uiBinder = GWT.create(CreateuserUiBinder.class);

    protected MemberServiceAsync memberService = GWT.create(MemberService.class);

    private UserStatus userStatus;

    private Member targetMember = null;

    interface CreateuserUiBinder extends UiBinder<Widget, Createuser>{
    }

    @UiField
    TextBox guildName;

    @UiField
    TextBox realmName;

    @UiField
    TextBox memberName;

    @UiField
    TextBox memberAccount;

    @UiField
    TextBox order;

    @UiField
    ListBox type;

    @UiField
    Button okButton;

    @UiField
    Button resetButton;

    public Createuser(UserStatus userStatus){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        setTextBox();
        setButton();
    }

    public Createuser(UserStatus userStatus, Member targetMember){
        initWidget(uiBinder.createAndBindUi(this));
        this.userStatus = userStatus;
        this.targetMember = targetMember;
        setTextBox();
        setTargetMemberInfo();
        setButton();
    }

    private void setTargetMemberInfo(){
        guildName.setText(targetMember.getGuild());
        realmName.setText(targetMember.getRealm());
        memberName.setText(targetMember.getMemberName());
        memberAccount.setText(targetMember.getMemberAccount());
        order.setText(String.valueOf(targetMember.getOrder()));

        String typeValue = targetMember.getMembertype();
        for(int i = 0;i < type.getItemCount();i++){
            String itemValue = type.getItemText(i);
            if(itemValue.equals(typeValue)){
                type.setSelectedIndex(i);
                break;
            }
        }

    }

    private void setTextBox(){
        if(!userStatus.isAdmin() && userStatus.isOfficer()){
            guildName.setReadOnly(true);
            guildName.setText(userStatus.getMember().getGuild());
            realmName.setReadOnly(true);
            realmName.setText(userStatus.getMember().getRealm());
        }
    }

    private void setButton(){
        okButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                setMember();

            }
        });

        resetButton.addClickHandler(new ClickHandler(){

            public void onClick(ClickEvent event){
                clearInfo();
            }
        });

    }

    protected void setMember(){
        if(targetMember == null){
            targetMember = new Member();
        }

        String guildNameString = guildName.getText();
        String realmNameString = realmName.getText();
        String typeString = type.getValue(type.getSelectedIndex());
        String memberNameString = memberName.getText();
        String memberAccountString = memberAccount.getText();
        String orderString = order.getText();

        errorString = CheckError.checkCreateUser(guildNameString, realmNameString, memberNameString, memberAccountString, orderString);

        if(errorString.equals("")){
            targetMember.setGuild(guildNameString);
            targetMember.setRealm(realmNameString);
            targetMember.setMembertype(typeString);
            targetMember.setMemberName(memberNameString);
            targetMember.setMemberAccount(memberAccountString);
            targetMember.setOrder(Long.valueOf(orderString));

            memberService.setMember(targetMember, new AsyncCallback<Void>(){

                public void onSuccess(Void result){
                    setInfoMessage("メンバーを追加/更新しました。");
                    clearErrorMessage();
                }

                public void onFailure(Throwable caught){
                    clearInfoMessage();
                    setErrorMessage("更新に失敗しました。");
                }
            });
        }else{
            clearInfoMessage();
            setErrorMessage(errorString);
        }
    }

    protected void clearInfo(){
        if(userStatus.isAdmin()){
            guildName.setText("");
            realmName.setText("");
        }
        type.setSelectedIndex(0);
        memberName.setText("");
        memberAccount.setText("@gmail.com");
    }

}
